<template>
  <div ref="userRankChart" style="width: 100%; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';
import { userPointsRank} from "@/api/system/config";

export default {
  mounted() {
    this.initUserRankChart();
  },
  data(){
    return{
      data:[]
    }
  },
  methods: {
    async initUserRankChart() {
      try {
        await userPointsRank().then(response => {
          this.data  = response.data
        });

        const option = {
          title: {
            text: '用户积分排行榜',
            left: 'center'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          xAxis: {
            type: 'category',
            data: this.data.map(item => item.nickName)
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '积分',
              type: 'bar',
              data: this.data.map(item => item.points)
            }
          ]
        };

        const chart = echarts.init(this.$refs.userRankChart);
        chart.setOption(option);
      } catch (error) {
        console.error('获取用户积分排名数据失败:', error);
      }
    }
  }
};
</script>
